package com.haitaoit.chengquan.module.center_service.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ServiceFanweiActivity extends BaseActivity {
    @ViewInject(R.id.fanwei_et)
    EditText fanwei_et;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_service_fanwei);
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        if ("1".equals(getIntent().getStringExtra("type"))) {
            setTitle_V("服务范围");
            fanwei_et.setHint("请设置您可提供的服务范围（可以具体到市级/区级，不同地区请用“，”号分开）");
        } else if ("2".equals(getIntent().getStringExtra("type"))) {
            setTitle_V("个人介绍");
            fanwei_et.setHint("请在此区域输入个人内容！");
        } else if ("3".equals(getIntent().getStringExtra("type"))) {
            setTitle_V("公司介绍");
            fanwei_et.setHint("请在此区域输入公司的介绍内容！");
        }
        fanwei_et.setText(getIntent().getStringExtra("fwfw"));
        // 光标在最后
        fanwei_et.setSelection(fanwei_et.getText().toString().length());
    }

    @OnClick({R.id.fanwei_save})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanwei_save:// 保存
                if (fanwei_et.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入内容！");
                } else {
                    if ("1".equals(getIntent().getStringExtra("type"))) {
                        httpGetupdate_user_service_scope();
                    } else if ("2".equals(getIntent().getStringExtra("type"))) {
                        httpGetupdate_user_company_introduction();
                    } else if ("3".equals(getIntent().getStringExtra("type"))) {
                        httpGetupdate_user_company_introduction();
                    }
                }
                break;
        }
        super.onClick(v);
    }

    // 服务范围
    private void httpGetupdate_user_service_scope() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("service_scope", fanwei_et.getText().toString()));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("service_scope", fanwei_et.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getupdate_user_service_scope, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            Intent i = new Intent();
                            i.putExtra("Fanwei", fanwei_et.getText().toString());
                            setResult(1, i);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 介绍
    private void httpGetupdate_user_company_introduction() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("company_introduction", fanwei_et.getText().toString()));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("company_introduction", fanwei_et.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getupdate_user_company_introduction, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            Intent i = new Intent();
                            i.putExtra("Fanwei", fanwei_et.getText().toString());
                            setResult(2, i);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
